#!/bin/bash
set -e
#set -v

cf login -a "${api}" -u "${username}" -p "${password}" -o "${org}" -s "${space}"
cf create-service-key "${service_instance}" flyway-service-key
SERVICE_KEY=$(cf service-key "${service_instance}" flyway-service-key | awk '!/flyway-service-key/')

DB_URL=$(echo "${SERVICE_KEY}" | jq -r '.jdbcUrl' )
DB_USER=$(echo "${SERVICE_KEY}"| jq -r '.databaseLogin')
DB_PASSWORD=$(echo "${SERVICE_KEY}"| jq -r '.databaseLoginPassword')

flyway -X migrate -configFiles="${flyway_config}" -user="${DB_USER}" -password="${DB_PASSWORD}" -url="${DB_URL}"

cf delete-service-key -f "${service_instance}" flyway-service-key